package com.hdaccp.alunbo.controller;

import com.hdaccp.alunbo.dao.lunboDao;
import com.hdaccp.pojo.po.lunbo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/lunbo")
public class lunboController {
    @Autowired
    public lunboDao lunboDao;

    /**
     * 查询全部
     * @return
     */
    @RequestMapping("/query")
    public List<lunbo> query(){
        return lunboDao.findAll();
    }

    /**
     * 修改
     * @param lunboId
     * @param img
     * @param paixu
     * @return
     */
    @RequestMapping("/updateLunbo/{lunboId}/{img}/{paixu}")
    public String updateLunbo(@PathVariable("lunboId") int lunboId, @PathVariable("img") String img, @PathVariable("paixu") int paixu){
        String message="修改成功";
        lunbo lunbo= lunboDao.findById(lunboId).get();


        lunbo.setImg(img);
        lunbo.setPaixu(paixu);
        try {
            lunboDao.save(lunbo);
        } catch (Exception e){
            message="失败了";
        }
        return message;
    }

    /**
     * 删除
     * @param LunboId
     * @return
     */
    @RequestMapping("deleteLunbo/{lunboId}")
    public String deleteLunbo(@PathVariable("lunboId") int  LunboId){
        String message = "成功了";

        try {
            lunboDao.deleteById(LunboId);
        } catch (Exception e){
             message="失败了";
    }
        return message;
    }

    /**
     * 添加
     * @param img
     * @param paixu
     * @return
     */
    @RequestMapping("/insertLunbo/{img}/{paixu}")
    public String insertLunbo(@PathVariable("img") String img,@PathVariable("paixu") int paixu){
        String message = "成功了";
        lunbo lunbo=new lunbo();
        lunbo.setImg(img);
        lunbo.setPaixu(paixu);
        try {
            lunboDao.save(lunbo);
        } catch (Exception e){
            message="失败了";
        }
        return message;
    }
}
